﻿using HtmlAgilityPack;
using System;
using System.Collections.Generic;
using System.Collections.ObjectModel;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using wenku8.Entity;
using wenku8.Enum;

namespace wenku8.Parser
{
    public class CommentParser
    {
        // 获取评论
        public static async Task GetCommentsAsync(ObservableCollection<Comment> comments, HtmlNodeCollection trNodes)
        {
            foreach (var trNode in trNodes) {
                var comment = new Comment();
                var tdNodes = trNode.Descendants("td").ToArray();
                var aNode = tdNodes[0].Descendants("a").Single();//SelectSingleNode(tdNodes[0].XPath + "/a");
                comment.CommentUri = aNode.GetAttributeValue("href", "");
                comment.Content = aNode.InnerText;

                aNode = tdNodes[2].Descendants("a").Single();
                var userUri = aNode.GetAttributeValue("href", "");
                comment.UserUri = userUri;
                comment.UserName = aNode.InnerText;

                var imageBaseUri = "https://www.wenku8.net/files/system/avatar/";
                var defaultHeadImageUri = "https://www.wenku8.net/images/noavatar.jpg";
                var userId = userUri.Substring(userUri.LastIndexOf('=') + 1);
                var headImageUri = imageBaseUri + userId.Substring(0, 3) + $"/{userId}.jpg";
                comment.HeadImage = await ImageHelper.GetImage(new Uri(headImageUri), ImageType.HeadImage)
                    ?? await ImageHelper.GetImage(new Uri(defaultHeadImageUri), ImageType.HeadImage);

                comment.Date = tdNodes[3].InnerText;
                var slices = tdNodes[1].InnerText.Split('/');
                comment.ReplayCount = slices[0].Replace("回复(", "").Replace(")", "");
                comment.ViewCount = slices[1].Replace("查看(", "").Replace(")", "");
                comments.Add(comment);
            }
        }

        // 获取评论的回复
        public static async void GetRepliesAsync(ObservableCollection<Comment> replies, HtmlNodeCollection tables)
        {
            for (var i = 0; i < tables.Count; i++) {
                var table = tables[i];
                var reply = new Comment
                {
                    Order = $"{i + 1}楼"
                };
                // //*[@id="content"]/table[4]/tbody/tr/td[2]/div[2]
                var tdNodes = table.Descendants("td").ToArray();
                var userinfoTd = tdNodes[0];
                var imgNode = userinfoTd.Descendants("img").Single();
                var imgUri = imgNode.GetAttributeValue("src", "https://www.wenku8.net/images/noavatar.jpg");
                reply.HeadImage = await ImageHelper.GetHeadImageAsync(imgUri);

                var strongNode = userinfoTd.Descendants("strong").Single();
                reply.UserName = strongNode.InnerText;

                var contentTd = tdNodes[1];
                var divNodes = contentTd.Descendants("div").ToArray();
                var contentNode = divNodes.Last();
                reply.Content = contentNode.InnerText;

                var dateNode = divNodes[1];
                var innerText = dateNode.InnerText;
                reply.Date = innerText.Substring(innerText.IndexOf('"') + 1, 19);
                
                replies.Add(reply);
            }
        }

        // 发送评论
        public static async Task<string> PostCommentAsync(string suri, List<KeyValuePair<string, string>> parameters)
        {
            var uri = new Uri(suri);
            return await HTMLParser.PostMessage(uri, parameters);
        }
    }
}
